package org.maxgroundstation.world.landmark;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * Utility class for landmarks.
 * @author Scott Davis
 * 
 * Copyright (C) 2012  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class LandmarkManager {

    // Static members.
    private static final String SAVE_FILE_PATH = "resources/landmarks/landmark_list.xml";
    
    // Data members.
    private List<Landmark> landmarks;
    
    /**
     * Constructor.
     */
    public LandmarkManager() {
        
        // Load landmarks.
        landmarks = loadLandmarks();
    }
    
    /**
     * Gets a list of all landmarks.
     * @return list of landmarks.
     */
    public List<Landmark> getLandmarks() {
        return new ArrayList<Landmark>(landmarks);
    }
    
    /**
     * Loads a list of landmarks from the XML file.
     * @return list of landmarks.
     */
    private List<Landmark> loadLandmarks() {
        List<Landmark> landmarkList = new ArrayList<Landmark>();
        
        InputStream stream = null;
        
        try {
            stream = getClass().getClassLoader().getResourceAsStream(SAVE_FILE_PATH);
            if (stream != null) {
                InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
                SAXBuilder saxBuilder = new SAXBuilder(false);
                Document inputDoc = saxBuilder.build(reader);
                
                Element landmarkListElement = inputDoc.getRootElement();
                
                Iterator i = landmarkListElement.getChildren().iterator();
                while (i.hasNext()) {
                    Element landmarkElement = (Element) i.next();
                   
                    String name = landmarkElement.getAttributeValue("name");
                    
                    Element descriptionElement = landmarkElement.getChild("description");
                    String description = descriptionElement.getTextTrim();
                    
                    double latitude = Double.parseDouble(landmarkElement.getAttributeValue("latitude"));
                    double longitude = Double.parseDouble(landmarkElement.getAttributeValue("longitude"));
                    
                    landmarkList.add(new Landmark(name, description, latitude, longitude));
                }
            }
            else {
                System.err.println(SAVE_FILE_PATH + " could not be found.");
            }
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
        finally {
            try {
                if (stream != null) stream.close();
            }
            catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
        
        return landmarkList;
    }
}